package com.tanguy.user.ui.trip.activity.work

import android.annotation.SuppressLint
import android.app.Activity
import android.view.View
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.ImmersionTitleActivity
import com.tanguy.user.bean.orde.TripOrder
import com.tanguy.user.custom.RippleImageView
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.window.MessageWindow
import com.tanguy.user.utlis.RxBus
import com.tanguy.user.utlis.extend.*
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import java.util.concurrent.TimeUnit

/**
 * Created by dab on 2017/12/1 0001 14:50
 * 等待接单
 * type 1.推单，2派单
 */
class WaitDriverActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_wait_driver
    private val rippleImageView by lazy { find<RippleImageView>(R.id.riv) }
    override fun initView() {
        super.initView()
        title = "等待应答"
        titleBar.showLeft(false)
        val orderId = intent.getStringExtra("orderId")
        val type = intent.getIntExtra("type", 0)
        if (type == 1) {
            HttpManager.getTravlOrderInfo(orderId, 1).requestSucceed(this, false) {
                it.orderId = orderId.toInt()
                startActivity<TripWorkActivity>("TripOrder" to it)
                setResult(Activity.RESULT_OK)
                finish()
            }
        }
        compositeDisposable.add(Observable.interval(0, 1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    val minute = it / 60
                    val second = it % 60
                    setText(R.id.tv_time, "${minute.toTimeComplement()}:${second.toTimeComplement()}")
                })
        titleBar.addRightButton("取消订单", 0, View.OnClickListener {
            showPopupWindow(MessageWindow(this, "确认取消订单？") {
                if (it) {
                    cancelOrder(orderId) {
                        finish()
                    }
                }
            })
        })
    }

    override fun initEvent() {
        super.initEvent()
        compositeDisposable.add(RxBus.instance.register<TripOrder>(Const.RxType.ORDER_GRAB)
                .subscribe {
                    it.orderId = intent.getStringExtra("orderId").toInt()
                    startActivity<TripWorkActivity>("TripOrder" to it)
                    setResult(Activity.RESULT_OK)
                    finish()
                })
//        val ada="{\"con\":{\"lon\":0,\"model\":\" A7\",\"phone\":\"18408269575\",\"score\":0,\"state\":5,\"plate\":\"浙N378338\",\"img\":\"http://101.37.148.119/resources/upload/images/1507371839660.jpg\",\"departure\":\"国防乐园\",\"dName\":\"看看\",\"departtime\":1512703440000,\"greadname\":\"经济型（宝马为主）\",\"km\":0,\"num\":0,\"bourn\":\"府城大道西段石羊街道新园社区\",\"money\":10,\"brand\":\" 宝马\",\"lat\":0},\"method\":\"USER_ORDER_GRAB\",\"code\":\"0\",\"msg\":\"SUCCESS\"}"
//        SocketManager.parseJson(ada)
    }

    override fun onBackPressed() {

    }

    @SuppressLint("MissingSuperCall")
    override fun onStart() {
        super.onStart()
        rippleImageView.startWaveAnimation()
    }

    @SuppressLint("MissingSuperCall")
    override fun onStop() {
        super.onStop()
        rippleImageView.stopWaveAnimation()
    }
}